#include <iostream>
#include <fstream>
using namespace std;
#include "contacts.pb.h"
using namespace google::protobuf;
void PrintContacts(contacts2::Contacts& contacts)
{
    for(int i = 0;i<contacts.people_size();i++)
    {
        cout<<"---------联系人"<<i+1<<"------"<<endl;

        const contacts2::PeopleInfo& people = contacts.people(i);
        cout<<"联系人姓名: "<<people.name()<<endl;
        cout<<"联系人年龄: "<<people.age()<<endl;
        for(int j = 0;j<people.phone_size();j++)
        {
            const contacts2::PeopleInfo_Phone& phone = people.phone(j);
            cout<<"联系人电话"<<j+1<<": "<<phone.number()<<endl;
        }
        //获取未知字段
        const Reflection* reflection = people.GetReflection();
        const UnknownFieldSet&  set = reflection->GetUnknownFields(people);
        for(int j = 0;j<set.field_count();j++)
        {
            const UnknownField& field = set.field(j);
            cout<<"未知字段"<<j+1<<": "<<"字段编号 "<<field.number()<<",";
            switch(field.type())
            {
                case UnknownField::TYPE_VARINT:
                    cout<<"值: "<<field.varint()<<endl;
                    break;
                case UnknownField::TYPE_LENGTH_DELIMITED:
                    cout<<"值: "<<field.length_delimited()<<endl;
                    break;
                //case....
                default:
                    break;
            }
        }


    }
}

int main()
{
    contacts2::Contacts contacts;
    //打开文件，读取通讯录的信息
    fstream input("../contacts.bin",ios::in | ios::binary);//因为文件中存储的是二进制的信息
    if(!contacts.ParseFromIstream(&input))
    {
        cerr<<"parse error"<<endl;
        input.close();
        return -1;
    }
    //打印
    PrintContacts(contacts);
    input.close();
    return 0;
}